package com.ellricky.baselib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatEditText;

import com.andr.common.util.ScreenUtil;
import com.andr.common.util.ViewUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.util.EAppUtil;

import java.util.Objects;

public class EditItemView extends RelativeLayout implements View.OnClickListener {


    private Context context;
    private TextView tvKey;
    private AppCompatEditText etValue;

    private float defaultH;
    private OnClickListener onClickListener;

    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public EditItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        View.inflate(context, R.layout.base_view_edit_item, this);
        tvKey = findViewById(R.id.tvKey);
        etValue = findViewById(R.id.etValue);
        defaultH = ScreenUtil.dpUnit(context, 40);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditItemView);

        String key = typedArray.getString(R.styleable.EditItemView_base_edit_key);
        String hint = typedArray.getString(R.styleable.EditItemView_base_edit_hint);
        boolean editable = typedArray.getBoolean(R.styleable.EditItemView_base_edit_editable, true);
        float height = typedArray.getDimension(R.styleable.EditItemView_base_edit_height, defaultH);

        typedArray.recycle();

        setHeight(height);
        setKey(key);
        setHint(hint);
        initEditable(editable);
    }


    public void setHeight(float height) {
        if (height != defaultH) {
            RelativeLayout.LayoutParams rlP = (LayoutParams) etValue.getLayoutParams();
            rlP.height = (int) height;
            etValue.setLayoutParams(rlP);
            etValue.setGravity(Gravity.TOP);
        }
    }


    public void setKey(String key) {
        if (TextUtils.isEmpty(key)) tvKey.setVisibility(GONE);
        else {
            tvKey.setVisibility(VISIBLE);
            tvKey.setText(key);
        }
    }

    public void setHint(String hint) {
        etValue.setHint(hint);
    }

    public void setValue(String value) {
        etValue.setText(value);
    }

    public void initEditable(boolean editable) {
        if (editable) {
            etValue.setBackgroundResource(R.drawable.base_bg_edit_input);
            hideNoInoutIcon();
            etValue.setFocusable(true);
            etValue.setFocusableInTouchMode(true);
            etValue.setCursorVisible(true);
            etValue.setOnClickListener(null);
        } else {
            etValue.setBackgroundResource(R.drawable.base_bg_edit_no_input);
            ViewUtil.setTextViewDrawable(context, etValue,
                    R.drawable.base_icon_edit_select, 2);
            etValue.setFocusable(false);
            etValue.setFocusableInTouchMode(false);
            etValue.setCursorVisible(false);
            etValue.setOnClickListener(this);
        }
    }

    public void setInputDecimal() {
        etValue.setInputType(
                InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL
        );
    }

    public void setInputNumber() {
        etValue.setInputType(
                InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL
        );
    }

    public void setInputPhone() {
        etValue.setInputType(
                InputType.TYPE_CLASS_PHONE
        );
    }

    public void setKeySize(float size) {
        tvKey.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        tvKey.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    public String getValue() {
        return Objects.requireNonNull(etValue.getText()).toString().trim();
    }

    @Override
    public void onClick(View view) {
        if (view == etValue) {
            if (onClickListener != null) onClickListener.onClick(this);
        }
    }

    public void hideNoInoutIcon() {
        ViewUtil.setTextViewDrawable(context, etValue, R.drawable.base_icon_edit_select, -1);
    }

    public void clearEditBg() {
        etValue.setBackground(null);
    }
}
